package infolayer2.graph;

import java.util.Iterator;
import java.util.Set;

import com.google.common.collect.Sets;

/**
 * Write access is only supported via Entity. Thus, all write
 * methods are package protected.
 */
public class EdgeSet implements Iterable<Edge> {
  
  public static EdgeSet EMPTY = new EdgeSet();
  
  Set<Edge> values = Sets.newHashSet();

  void add(Edge edge) {
    values.add(edge);
  }
  
  public String toString() {
    switch (values.size()) {
    case 0:
      return "[]";
    case 1:
      return values.iterator().next().toString();
    default:
      StringBuilder sb = new StringBuilder("[");
      String sep = "";
      for (Edge value: values) {
        sb.append(sep);
        sb.append("\n    ");
        sb.append(value.toString());
        sep = ",";
      }
      sb.append("\n  ]" );
      return sb.toString();
    }
  }

  public boolean contains(Object object) {
    for (Edge value : values) {
      if (object.equals(value.value) || object.equals(value.to)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public Iterator<Edge> iterator() {
    return values.iterator();
  }

  public int size() {
    return values.size();
  }
}
